﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
	//
	public class RoleSkill extends MovieClip
	{
		public static const NAME:String = "Role-Skill";

		private var effectVol:int = 100;
		private var overLay:OverLay;

		private var skillList:DataGrid;

		public function RoleSkill()
		{
			stage ? init(null):addEventListener(Event.ADDED_TO_STAGE,init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			overLay = new OverLay();
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);

			skillList = new DataGrid();
			skillList.x = 5;
			skillList.y = 70;
			skillList.width = 590;
			skillList.height = 290;
			this.addChild(skillList);

			btnArmy.visible = false;

			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnDetail.addEventListener(MouseEvent.CLICK, onClick);
			btnGoods.addEventListener(MouseEvent.CLICK, onClick);
			btnItem.addEventListener(MouseEvent.CLICK, onClick);
			btnArmy.addEventListener(MouseEvent.CLICK, onClick);

			mcFrame.reSize(600, 450);

			_resizeUI();
		}

		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			if (params.Name == "GetRoleSkill")
			{
				var style:TextFormat = new TextFormat();
				style.color = 0xff6600;
				style.size = 16;
				style.font = "LiSu";
			
				skillList.setRendererStyle("textFormat", style);
			
				var c0:DataGridColumn = new DataGridColumn("ID");
				var c1:DataGridColumn = new DataGridColumn("名称");
				var c2:DataGridColumn = new DataGridColumn("类别");
				var c3:DataGridColumn = new DataGridColumn("等级");
				var c4:DataGridColumn = new DataGridColumn("经验");
				
				var type:String;
				
				c0.visible = false;
				c1.width = 290;
				c2.width = 100;
				c3.width = 100;
				c4.width = 100;
				
				skillList.addColumn(c0);
				skillList.addColumn(c1);
				skillList.addColumn(c2);
				skillList.addColumn(c3);
				skillList.addColumn(c4);

				for (var i:int = 0; i < params.Process.length; i++)
				{
					switch (params.Process[i].Type)
					{
						case 0:
							type = "";
							break;
						case 1:
							type = "<生产类>";
							break;
						case 2:
							type = "<经营类>";
							break;
						case 3:
							type = "<内政类>";
							break;
						case 4:
							type = "<军事类>";
							break;
						case 5:
							type = "<舌战类>";
							break;
						case 6:
							type = "<单挑类>";
							break;
						case 7:
							type = "<计略类>";
							break;
						case 8:
							type = "<战法类>";
							break;
						case 9:
							type = "<称号类>";
							break;
					}

					skillList.addItem({"ID":params.Process[i].SID, "名称":params.Process[i].Name, "类别":type, "等级":params.Process[i].Rank, "经验":params.Process[i].Exp});
				}
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);

			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnDetail.removeEventListener(MouseEvent.CLICK, onClick);
			btnGoods.removeEventListener(MouseEvent.CLICK, onClick);
			btnItem.removeEventListener(MouseEvent.CLICK, onClick);
			btnArmy.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;

			this.x = (stage.stageWidth - this.width) * .5
			;
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}

			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);

			switch (evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "RoleSkill"]));
					break;
				case "btnDetail":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleDetail"]]));
					break;
				case "btnGoods" :
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleGoods"]]));
					break;
				case "btnItem" :
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleItem"]]));
					break;
				case "btnArmy" :
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleArmy"]]));
					break;
			}
		}
	}
}